package com.kldy8.kldyw.find;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;
import com.bgthrid.fragment.RecyclerViewFragment;
import com.kldy8.kldyw.R;
import com.kldy8.kldyw.system.ServerApi;

public class NewInfoActivity extends AppCompatActivity
{
    Toolbar toolbar;
    PagerSlidingTabStrip tabs;
    ViewPager pager;

    private MyPagerAdapter adapter;
    private int currentColor;
    private Drawable oldBackground = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_info);
        init();
    }

    private void  init()
    {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("头条");//设置主标题
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true); // 决定左上角图标的右侧是否有向左的小箭头, true
        // 有小箭头，并且图标可以点击
        actionBar.setDisplayShowHomeEnabled(false);

        tabs  = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        pager  = (ViewPager) findViewById(R.id.vp_view);

        setSupportActionBar(toolbar);
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        tabs.setViewPager(pager);
        tabs.setIndicatorColor(getResources().getColor(R.color.colorAccent));
//        tabs.setPstsIndicatorColor(getResources().getColor(R.color.colorAccent));

        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                .getDisplayMetrics());
        pager.setPageMargin(pageMargin);
        pager.setCurrentItem(0);
//        changeColor(ContextCompat.getColor(getBaseContext(), R.color.green));

        tabs.setOnTabReselectedListener(new PagerSlidingTabStrip.OnTabReselectedListener() {
            @Override
            public void onTabReselected(int position) {
                Toast.makeText(NewInfoActivity.this, "Tab reselected: " + position, Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_notbtn, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if (item.getItemId() == android.R.id.home)
        {
            finish();
            return true ;
        }

        return super.onOptionsItemSelected(item);
    }

    private void changeColor(int newColor) {
        tabs.setBackgroundColor(newColor);
        // change ActionBar color just if an ActionBar is available
        Drawable colorDrawable = new ColorDrawable(newColor);
        Drawable bottomDrawable = new ColorDrawable(ContextCompat.getColor(getBaseContext(), android.R.color.transparent));
        LayerDrawable ld = new LayerDrawable(new Drawable[]{colorDrawable, bottomDrawable});
        if (oldBackground == null) {
            getSupportActionBar().setBackgroundDrawable(ld);
        } else {
            TransitionDrawable td = new TransitionDrawable(new Drawable[]{oldBackground, ld});
            getSupportActionBar().setBackgroundDrawable(td);
            td.startTransition(200);
        }

        oldBackground = ld;
        currentColor = newColor;
    }

    public void onColorClicked(View v)
    {
        int color = Color.parseColor(v.getTag().toString());
        changeColor(color);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("currentColor", currentColor);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        currentColor = savedInstanceState.getInt("currentColor");
        changeColor(currentColor);
    }


    public class MyPagerAdapter extends FragmentPagerAdapter
    {

        private final String[] TITLES = {getResources().getString(R.string.film_information),
                getResources().getString(R.string.film_experience),
                getResources().getString(R.string.film_observation) ,
                getResources().getString(R.string.film_character), getResources().getString(R.string.film_training), getResources().getString(R.string.film_seeding)};


        MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position)
        {
             switch (position % 6)
            {
                case 0:
                    RecyclerViewFragment recyclerViewFragmentI = RecyclerViewFragment.newInstance(ServerApi.ID_NEWSS_NEWS);
                    return recyclerViewFragmentI;
                case 1:
                    RecyclerViewFragment recyclerViewFragmentII = RecyclerViewFragment.newInstance(ServerApi.ID_NEWSS_EXPERIENCE);
                    return recyclerViewFragmentII;
                case 2:
                    RecyclerViewFragment recyclerViewFragmentIII = RecyclerViewFragment.newInstance(ServerApi.ID_NEWSS_OBSERVATION);
                    return recyclerViewFragmentIII;
                case 3:
                    RecyclerViewFragment recyclerViewFragmentIV = RecyclerViewFragment.newInstance(ServerApi.ID_NEWSS_CHARACTER);
                    return recyclerViewFragmentIV;
                case 4:
                    RecyclerViewFragment recyclerViewFragmentV = RecyclerViewFragment.newInstance(ServerApi.ID_NEWSS_TRAINING);
                    return recyclerViewFragmentV;
                case 5:
                    RecyclerViewFragment recyclerViewFragmentVI = RecyclerViewFragment.newInstance(ServerApi.ID_NEWSS_SEEDING);
                    return recyclerViewFragmentVI;
                default:
                    RecyclerViewFragment recyclerViewFragment = RecyclerViewFragment.newInstance(ServerApi.ID_NEWSS_NEWS);
                    return recyclerViewFragment;
            }
        }
    }
}
